﻿using Newtonsoft.Json;
using SouthKoreaCoffee.CoffeeModel;
using SouthKoreaCoffee.IceModel;
using SouthKoreaCoffee.IOModel;
using SouthKoreaCoffee.Model;
using SouthKoreaCoffee.RobotModel;
using SouthKoreaCoffee.SocketClientsModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static SouthKoreaCoffee.SocketClientsModel.CoffeeModels;
using static System.Net.Mime.MediaTypeNames;

namespace SouthKoreaCoffee
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool isMake = true;
        /// <summary>
        /// 基本配置存储位置
        /// </summary>
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"IPorPortMessage.txt";
        /// <summary>
        /// M12警告
        /// </summary>
        public static string WarnPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Warn";
        /// <summary>
        /// M12故障
        /// </summary>
        public static string BreakPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"BreakDowm";
        /// <summary>
        /// 正在制作
        /// </summary>
        public static List<CoffeeMakeModel> coffeeMaking { get; set; } = new List<CoffeeMakeModel>();
        public static int zsInit = 0;
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public static void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                    if (data != null)
                    {
                        Console.WriteLine(data);
                        Console.WriteLine(isMake);
                        if (isMake)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsgs>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        if (isMake)
                                        {
                                            if (list.Count > 0)
                                            {
                                                var ddd = list.FirstOrDefault(c => c.typeList2.Any(a => (a.status == "3" || a.status == "4") && !coffeeMaking.Any(b => b.OrderNum == c.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId)));
                                                if (ddd == null) continue;
                                                if (ddd.typeList2.Count > 0)
                                                {

                                                    var make = ddd.typeList2.FirstOrDefault(a => (a.status == "3" || a.status == "4") && !coffeeMaking.Any(b => b.OrderNum == ddd.orderNum && a.orderId == b.OrderId && a.id == b.OrderGoodsId));
                                                    if (make != null)
                                                    {
                                                        Console.WriteLine(make.num.ToString() + " " + DateTime.Now.ToString());
                                                        if (make.jsonCodeVal.Length > 4)
                                                        {
                                                            var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make.jsonCodeVal);
                                                            if (coffeecode.Count > 0)
                                                            {
                                                                var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                if (coffeeID != null)
                                                                {
                                                                    bool robotL = RobotModelLeft.Instance.LRobotJQRYX_Sel();
                                                                    if (robotL)
                                                                    {
                                                                        //if (coffeeMsg.Instance.isBreakDown && coffeeMsg.Instance.isWarn)
                                                                        CoffeeMaking(coffeeID.classCode, make, ddd.orderNum);
                                                                        Console.WriteLine("一单完成！");
                                                                        // CoffeeMaking("5003", null, "5003");
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }
        }
        public static void CoffeeMaking(string makecoffeeName, DeviceOrderQueue list, string orderNum, bool CoffeeBol = true)
        {
            try
            {
                if (!isMake) return;
                isMake = false;
                int sugarCode = 0;//糖型
                int cupCode = 0;//杯型
                double sugerV = 1;//糖量
                int prepaidCode = 0;//预存
                int code = 0;
                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                Console.WriteLine(DateTime.Now.ToString() + "  cx10" + list.jsonCodeVal);
                if (msgs != null)
                {
                    if (msgs.Any(a1 => a1.SyrupCode != ""))
                    {
                        sugarCode = msgs.Any(a1 => a1.SyrupCode == "1") ? 1 : msgs.Any(a1 => a1.SyrupCode == "2") ? 2 : msgs.Any(a1 => a1.SyrupCode == "3") ? 3 : msgs.Any(a1 => a1.SyrupCode == "4") ? 4 : msgs.Any(a1 => a1.SyrupCode == "5") ? 5 : msgs.Any(a1 => a1.SyrupCode == "6") ? 6 : 0;
                    }
                    if (msgs.Any(b => b.CupType != ""))
                    {
                        cupCode = msgs.Any(b => b.CupType == "1") ? 1 : msgs.Any(b => b.CupType == "2") ? 2 : msgs.Any(b => b.CupType == "0") ? 0 : 4;
                    }
                    if (msgs.Any(b => b.SyrupLevel != ""))
                    {
                        sugerV = msgs.Any(b => b.SyrupLevel == "1") ? 1 : msgs.Any(b => b.SyrupLevel == "2") ? 0.5 : msgs.Any(b => b.SyrupLevel == "3") ? 1.5 : 1;
                    }
                    if (msgs.Any(a => a.classCode != ""))
                    {
                        var cod = msgs.FirstOrDefault(a => a.classCode != "");
                        code = int.TryParse(cod.classCode, out int clas) ? clas : 58001;
                    }
                }
                if (!WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol && !WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                {
                    isMake = true;
                    return;
                }
                if (!WuLiaoCoffeeSel.Instance.LidFeed1_Sel_Bol || !(WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol || WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol))
                {
                    isMake = true;
                    return;
                }
                while (true)
                {
                    EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                    status.orderGoodsId = list.id;
                    status.orderId = list.orderId;
                    status.status = 4;
                    string lists = JsonConvert.SerializeObject(status);
                    var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (ddd1 != null)
                    {
                        if (ddd1 != "")
                        {
                            var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                            if (list10 != null)
                            {
                                if (list10.code == "0" || list10.code == "1")
                                {
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    break;
                                }
                            }
                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                        }

                    }
                    Thread.Sleep(100);
                }
                Console.WriteLine("***" + list.orderId + "&" + list.id + "&" + orderNum + "#1");
                if (WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol)
                {
                    ClientList clientList5 = new ClientList();
                    clientList5.MsgBol = WuLiaoCoffeeSel.Instance;
                    clientList5.code = 12;
                    ClientDP.Instance.orderNum1 = orderNum;
                    ClientDP.Instance.OrderId1 = list.orderId;
                    ClientDP.Instance.IsMakeInit1 = 1;
                    clientList5.DP = ClientDP.Instance;
                    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList5)));

                }
                else if (WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                {
                    ClientList clientList3 = new ClientList();
                    clientList3.MsgBol = WuLiaoCoffeeSel.Instance;
                    clientList3.code = 12;
                    ClientDP.Instance.orderNum2 = orderNum;
                    ClientDP.Instance.OrderId2 = list.orderId;
                    ClientDP.Instance.IsMakeInit2 = 1;
                    clientList3.DP = ClientDP.Instance;
                    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList3)));

                }
                Thread.Sleep(500);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 10;
                clientList.IsMake = false;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                int MidCode = code / 100;
                int CoffeeCode = code % 100;
                Thread.Sleep(200);
                if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                    RobotModelLeft.Instance.RobotQJB1_UP();
                else
                    RobotModelLeft.Instance.RobotQJB2_UP();
                if (MidCode == 582)
                {
                    if (CoffeeCode == 5 || CoffeeCode == 6 || CoffeeCode == 7)
                        RobotModelLeft.Instance.RobotZJGTBSP_UP();
                    else
                        RobotModelLeft.Instance.RobotJBP_UP();
                    Thread.Sleep(200);
                }
                else
                    RobotModelLeft.Instance.RobotZJKFJ_UP();
                if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3 || sugarCode == 4 || sugarCode == 5 || sugarCode == 6)
                {
                    Thread.Sleep(200);
                    RobotModelLeft.Instance.RobotQJGT_UP();
                }
                Thread.Sleep(200);
                RobotModelLeft.Instance.RobotKFJL_UP();
                Thread.Sleep(500);
                CoffeeModels.Instance.QBMsg(cupCode);
                Thread.Sleep(200);
                while (true)
                {
                    while (!IOMsg.Instance.CFBol) { Thread.Sleep(10); }
                    Thread.Sleep(500);
                    if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol || WuLiaoCoffeeSel.Instance.TakeCup2_Sel_Bol)
                    {
                        Thread.Sleep(100);
                        RobotModelLeft.Instance.RobotJBCG_UP();
                        Thread.Sleep(100);
                        if (sugarCode == 1 || sugarCode == 2 || sugarCode == 3 || sugarCode == 4 || sugarCode == 5 || sugarCode == 6)
                        {
                            while (true)
                            {
                                bool RobotBol = false;
                                Thread.Sleep(200);
                                RobotBol = RobotModelLeft.Instance.LRobotDGTD_Sel();
                                Thread.Sleep(200);
                                if (RobotBol)
                                {
                                    Thread.Sleep(200);
                                    RobotModelLeft.Instance.RobotQJGT_Down();
                                    Thread.Sleep(200);
                                    break;
                                }
                            }
                            while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                            IOMsg.Instance.CFBol = false;
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            if (sugarCode == 1)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit1 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_end);
                            }
                            else if (sugarCode == 2)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit2 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_end);
                            }
                            else if (sugarCode == 3)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit3 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_end);
                            }
                            else if (sugarCode == 4)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT4_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit4 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT4_end);
                            }
                            else if (sugarCode == 5)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT5_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit5 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT5_end);
                            }
                            else if (sugarCode == 6)
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT6_start);
                                Thread.Sleep(Convert.ToInt32(WuLiaoCoffeeSel.Instance.SugarInit6 * 1000 * sugerV));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT6_end);
                            }
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            IOMsg.Instance.CFBol = true;
                            Thread.Sleep(1000);
                            RobotModelLeft.Instance.RobotJGTWC_UP();
                            Thread.Sleep(200);
                        }
                        if (MidCode == 582)
                        {
                            try
                            {
                                Thread.Sleep(200);
                                RobotModelLeft.Instance.RobotQZBJ_UP();
                                while (true)
                                {
                                    bool RobotBol = false;
                                    Thread.Sleep(200);
                                    RobotBol = RobotModelLeft.Instance.LRobotDZBJ_Sel();
                                    Thread.Sleep(200);
                                    if (RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(200);
                                if (WuLiaoCoffeeSel.Instance.Ice1_Sel_Bol)
                                {
                                    if (CoffeeCode == 5 || CoffeeCode == 6 || CoffeeCode == 7)
                                    {
                                        IceMakingMsg1.Instance.AllBol = false;
                                        Thread.Sleep(1000);
                                        IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                        Thread.Sleep(1000);
                                        Thread.Sleep(4000);
                                        IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.WaterMaking2);
                                        Thread.Sleep(4000);
                                    }
                                    else
                                    {
                                        IceMakingMsg1.Instance.AllBol = false;
                                        Thread.Sleep(1000);
                                        IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                        Thread.Sleep(1000);
                                        IceMakingMsg1.Instance.AllBol = true;
                                        Thread.Sleep(4000);
                                        for (int i = 0; i < 2; i++)
                                        {
                                            if (!WuLiaoCoffeeSel.Instance.Weigh1_Sel_Bol)
                                            {
                                                IceMakingMsg1.Instance.AllBol = false;
                                                Thread.Sleep(1000);
                                                IceMakingSerialPort1.Instance.SerialSend(IceMakingModel.Instance.IceMaking3);
                                                Thread.Sleep(1000);
                                                IceMakingMsg1.Instance.AllBol = true;
                                                Thread.Sleep(2000);
                                            }
                                        }
                                    }
                                }
                                Thread.Sleep(200);
                                RobotModelLeft.Instance.RobotJBWC2_UP();
                                Thread.Sleep(200);
                                RobotModelLeft.Instance.RobotQZBJ_Down();
                            }
                            catch (Exception)
                            {
                            }
                        }
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotJBCG_Down();
                        Thread.Sleep(200);
                        Console.WriteLine(DateTime.Now.ToString() + "  cx17");
                        Console.WriteLine(DateTime.Now.ToString() + "  cx18");
                        break;
                    }
                    else
                    {
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotWBTZ_UP();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotQBJC_Down();
                        Thread.Sleep(200);
                        CoffeeModels.Instance.QBMsg(cupCode);
                    }
                }
                if (MidCode == 582 && (CoffeeCode == 5 || CoffeeCode == 6 || CoffeeCode == 7))
                {

                }
                else
                {
                    while (!CoffeeMsg.Instance.isMaking) { Thread.Sleep(10); }
                    Thread.Sleep(200);
                    RobotModelLeft.Instance.RobotQKFJ_UP();
                    Thread.Sleep(200);
                    if (code == 58002 || code == 58202)
                    {
                        RobotModelLeft.Instance.RobotJGTWC_Down();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotJBWC_Down();
                    }
                    else
                    {
                        while (true)
                        {
                            bool RobotBol = false;
                            Thread.Sleep(200);
                            RobotBol = RobotModelLeft.Instance.LRobotDKFJ_Sel();
                            Thread.Sleep(200);
                            if (RobotBol)
                            {
                                RobotModelLeft.Instance.RobotJGTWC_Down();
                                Thread.Sleep(200);
                                RobotModelLeft.Instance.RobotJBWC_Down();
                                break;
                            }
                        }
                    }
                    CoffeeMsg.Instance.AllBol = false;
                    Thread.Sleep(200);
                    //CoffeeMakeNow(2);
                    CoffeeMsg.Instance.CoffeeMakeNow(code, sugerV);
                    Thread.Sleep(500);
                    CoffeeMsg.Instance.AllBol = true;

                    //while (isYC)
                    //{
                    //    Thread.Sleep(100);
                    //}
                    Thread.Sleep(1000);
                    while (CoffeeMsg.Instance.isMaking && (CoffeeMsg.Instance.isBreakDown && CoffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                    Thread.Sleep(1000);
                    while (!CoffeeMsg.Instance.isMaking && (CoffeeMsg.Instance.isBreakDown && CoffeeMsg.Instance.isWarn)) { Thread.Sleep(10); }
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotQKFJ_Down();
                    Thread.Sleep(100);
                    RobotModelLeft.Instance.RobotJKFWC_UP();
                }
                Thread.Sleep(100);
                RobotModelLeft.Instance.RobotQJG_UP();
                Thread.Sleep(200);
                while (true)
                {
                    bool RobotBol = false;
                    Thread.Sleep(200);
                    RobotBol = RobotModelLeft.Instance.LRobotDJGD_Sel();
                    Thread.Sleep(200);
                    if (RobotBol)
                    {
                        RobotModelLeft.Instance.RobotQJG_Down();
                        break;
                    }
                }
                Thread.Sleep(200);
                CoffeeModels.Instance.YGMsg(cupCode);
                Thread.Sleep(200);
                int CCInit = 0;
                if (WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol)
                {
                    RobotModelLeft.Instance.RobotQCCK1_UP();
                    CCInit = 1;
                }
                else if (WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                {
                    RobotModelLeft.Instance.RobotQCCK2_UP();
                    CCInit = 2;
                }
                if (CCInit != 0)
                {
                    while (true)
                    {
                        bool RobotBol = false;
                        Thread.Sleep(200);
                        RobotBol = RobotModelLeft.Instance.LRobotJFWC_Sel();
                        Thread.Sleep(200);
                        if (RobotBol)
                        {
                            if (CCInit == 1)
                            {
                                RobotModelLeft.Instance.RobotQCCK1_Down();
                            }
                            else if (CCInit == 2)
                            {
                                RobotModelLeft.Instance.RobotQCCK2_Down();
                            }
                            break;
                        }
                    }
                    TestDelegateInvoke testDelegate = new TestDelegateInvoke(CoffeeModels.Instance.MakeMealMsg);
                    testDelegate.BeginInvoke(orderNum, CCInit, list.id, list.orderId, null, null);
                    Console.WriteLine("出餐结束");
                    Console.WriteLine("这单完事");
                }
                IOMsg.Instance.QK();
                GC.Collect();
                bool robotL1 = RobotModelLeft.Instance.LRobotJQRYX_Sel();
                Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                if (robotL1)
                {
                    if (WuLiaoCoffeeSel.Instance.RobotWarn != true)
                    {

                        WuLiaoCoffeeSel.Instance.RobotWarn = true;

                    }
                }
                else
                {
                    if (WuLiaoCoffeeSel.Instance.RobotWarn != false)
                    {
                        WuLiaoCoffeeSel.Instance.RobotWarn = false;

                    }
                }
                CoffeeMakeModel makeModel = new CoffeeMakeModel();
                makeModel.OrderGoodsId = list.id;
                makeModel.OrderId = list.orderId;
                makeModel.OrderNum = orderNum;
                coffeeMaking.Add(makeModel);
                ClientList clientList2 = new ClientList();
                clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList2.code = 2;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                isMake = true;
            }
            catch (Exception)
            {
                return;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.url_start = porPortInfo.url_start;
                    IPorPortInfo.Instance.DeviceId = porPortInfo.DeviceId;
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    IPorPortInfo.Instance.SugarInit3 = porPortInfo.SugarInit3;
                    IPorPortInfo.Instance.SugarInit4 = porPortInfo.SugarInit4;
                    IPorPortInfo.Instance.SugarInit5 = porPortInfo.SugarInit5;
                    IPorPortInfo.Instance.SugarInit6 = porPortInfo.SugarInit6;
                    WuLiaoCoffeeSel.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    WuLiaoCoffeeSel.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    WuLiaoCoffeeSel.Instance.SugarInit3 = porPortInfo.SugarInit3;
                    WuLiaoCoffeeSel.Instance.SugarInit4 = porPortInfo.SugarInit4;
                    WuLiaoCoffeeSel.Instance.SugarInit5 = porPortInfo.SugarInit5;
                    WuLiaoCoffeeSel.Instance.SugarInit6 = porPortInfo.SugarInit6;
                }
                if (!Directory.Exists(WarnPath))
                {
                    Directory.CreateDirectory(WarnPath);
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                WarnPath = WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                if (!Directory.Exists(BreakPath))
                {
                    Directory.CreateDirectory(BreakPath);
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                BreakPath = BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();
                //Thread thread = new Thread(EWMMsg.Instance.IOSerial_Thr);
                //thread.IsBackground = true;
                //thread.Start();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
